package com.kexun.servlet.admin;

import com.alibaba.fastjson.JSON;
import com.kexun.pojo.Course;
import com.kexun.service.AdmService;
import com.kexun.service.impl.AdmServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/Servletaddcourse")
public class ServletaddCourse extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");


        Map<String, String[]> map = request.getParameterMap();
        Course course = new Course();
        try {
            BeanUtils.populate(course,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        AdmService as = new AdmServiceImpl();
        int i = as.addCourse(course);

        if(i>0){
            response.getWriter().print("添加成功");
//            response.setHeader("refresh","1;url=admin/courseAllInfo.jsp");
            request.getRequestDispatcher("admin/courseAllInfo.jsp").forward(request,response);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
